﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class CourseSubject
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }

        public int CourseId { get; set; }
        public string CourseName { get; set; }

        public int CourseDetailId { get; set; }
        public string CourseDetailName { get; set; }

        public int SubjectId { get; set; }
        public string SubjectName { get; set; }

        public string Description { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class CourseSubjectList : List<CourseSubject>
    {
        public MyError Error { get; set; }
        public CourseSubjectList()
        {
        }
    }

    public class CourseSubjectDB
    {
        MyDatabase _db = new MyDatabase();

        public CourseSubject Save(CourseSubject CourseSubject)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", CourseSubject.Id);
                _param.Add("@CourseDetailID", CourseSubject.CourseDetailId);
                _param.Add("@SubjectID", CourseSubject.SubjectId);
                if (CourseSubject.Description != string.Empty)
                {
                    _param.Add("@Description", CourseSubject.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@IsActive", CourseSubject.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_CourseSubjectInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    CourseSubject.Error.ErrorNo = 0;
                }
                else
                {
                    CourseSubject.Error.ErrorNo = 1;
                    CourseSubject.Error.ErrorMessage = "No row affect when saving school's CourseSubject.";
                }
            }
            catch (Exception Ex)
            {
                CourseSubject.Error.ErrorNo = 10002;
                CourseSubject.Error.ErrorLocation = "CourseSubject";
                CourseSubject.Error.ErrorMessage = Ex.Message;
                CourseSubject.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CourseSubject");
            }

            return CourseSubject;
        }

        public CourseSubjectList GetCourseSubjectList(int CompanyId)
        {
            CourseSubjectList retValue = new CourseSubjectList();

            try
            {
                DataTable _dt = new DataTable("tblCourseSubject");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_CourseSubjectSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        CourseSubject _CourseSubject = new CourseSubject();
                        FillData(_dr, _CourseSubject);
                        retValue.Add(_CourseSubject);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "CourseSubject";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CourseSubject");
            }

            return retValue;
        }

        public CourseSubject GetCourseSubject(int Id)
        {
            CourseSubject retValue = new CourseSubject();

            try
            {

                DataTable _dt = new DataTable("tblCourseSubject");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_CourseSubjectSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "CourseSubject";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CourseSubject");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, CourseSubject CourseSubject)
        {
            CourseSubject.Error = new MyError();
            CourseSubject.Id = Convert.ToInt32(aRow["Id"].ToString());
            CourseSubject.CompanyId = Convert.ToInt32(aRow["CompanyId"].ToString());
            CourseSubject.CourseId = Convert.ToInt32(aRow["CourseId"].ToString());
            CourseSubject.CourseDetailId = Convert.ToInt32(aRow["CourseDetailId"].ToString());
            CourseSubject.SubjectId = Convert.ToInt32(aRow["SubjectId"].ToString());
            
            if (aRow["Description"] != DBNull.Value)
            {
                CourseSubject.Description = aRow["Description"].ToString();
            }
            else
            {
                CourseSubject.Description = string.Empty;
            }

            CourseSubject.CompanyName = aRow["CompanyName"].ToString();
            CourseSubject.CourseName = aRow["CourseName"].ToString();
            CourseSubject.CourseDetailName = aRow["CourseDetailName"].ToString();
            CourseSubject.SubjectName = aRow["SubjectName"].ToString();

            CourseSubject.CreatedDate = Convert.ToDateTime(aRow["CreatedDate"].ToString());
            CourseSubject.ModifiedDate = Convert.ToDateTime(aRow["ModifiedDate"].ToString());

            CourseSubject.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            CourseSubject.Error.ErrorNo = 0;
        }
    }
}
